#include "ConstraintItem.h"

namespace Xps
{
	ConstraintItem::ConstraintItem() : Item(XpsAssets::TreeNode::CONSTRAINT)
	{
		truenode = NULL;
		falsenode = NULL;
		color = Qt::lightGray;
		font.setPointSize(26);
		textFlags = Qt::AlignCenter;
		size.setWidth(32);
		size.setHeight(32);
		pen.setColor(Qt::darkGray);
	}

	void ConstraintItem::swap() 
	{
		hiddenSwap();

		updateChildMarker();
	}

	
	void ConstraintItem::hiddenSwap() 
	{
		Item* tmp = truenode; 
		truenode = falsenode; 
		falsenode = tmp;

	}


	QMap<int, XpsAssets::ConstraintAttribute*>* ConstraintItem::attributes()
	{
		return &_attributes;
	}

	void ConstraintItem::updateChildMarker()
	{
		if (truenode != NULL)
			truenode->markTrue();

		if (falsenode != NULL)
			falsenode->markFalse();
	}

	void ConstraintItem::removeChildMarker()
	{
		if (truenode != NULL)
			truenode->markNone();

		if (falsenode != NULL)
			falsenode->markNone();
	}
	bool ConstraintItem::addChildNode(Item* item)
	{
		if (truenode == NULL)
		{
			truenode = item; 
			return true;
		}

		if (falsenode == NULL)
		{
			falsenode = item; 
			return true;
		}

		return false;
	}

	bool ConstraintItem::removeChildNode(Item* item)
	{
		if (truenode == item)
		{
			truenode = NULL; 
			return true;
		}

		if (falsenode == item)
		{
			falsenode = NULL; 
			return true;
		}

		return false;
	}
		
	void ConstraintItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
	{
		painter->setClipRect(option->exposedRect);

		if (option->state & QStyle::State_MouseOver)
		{
			paintHoverRect(painter);
		}
		 if (option->state & QStyle::State_Selected)
		{
			paintSelectionRect(painter);
		}
		
		QPixmap question(":/rs/images/constraint.png");
		painter->drawPixmap(QRect(0, 0, size.width(), size.height()), question, QRect(0, 0, 64, 64));
		
		Item::paint(painter, option, widget);
	}
}